package cn.wikiflyer.beikang.act.home;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.product.ProductInfoAct;
import cn.wikiflyer.beikang.bean.HomeBean.HomeNewsBean;
import cn.wikiflyer.beikang.bean.NewsListBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.utils.WKTimeUtils;
import cn.wk.libs4a.view.WKWebAct;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 新闻列表
 */
public class NewsListAct extends BaseActivity implements WKListViewInterface {

	private WKListViewUtils<HomeNewsBean, NewsListBean> listUtils;

	@ViewInject(id = R.id.store_linear)
	private LinearLayout store_linear;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_home_news_list);
	}

	private void initList() {
		PullToRefreshListView pList = (PullToRefreshListView) findViewById(R.id.wk_pull_refresh_list);
		listUtils = new WKListViewUtils<HomeNewsBean, NewsListBean>() {
		};
		listUtils.init(ctx, this, pList);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void init() {
		initList();
	}

	@Override
	public void setData() {
		listUtils.updateData();
	}

	/****************************************************************************
	 * WKListView 需实现的方法
	 ***************************************************************************/

	@Override
	public String getData(boolean... isLocal) {
		return app().net.getNews(listUtils.page + "", listUtils.pageCount + "",
				isLocal);
	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<NewsListBean>>() {
		}.getType();
		Gson gson = new Gson();
		listUtils.bean = gson.fromJson(result, cvbType);
		listUtils.list.addAll(listUtils.bean.data.list);
		listUtils.adapter.notifyDataSetChanged();
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.act_home_news_item;
	}

	class MyViewHolder extends WKViewHolder<HomeNewsBean> {

		View item_view;
		ImageView img_logo;
		TextView tv_name;
		TextView tv_part;
		TextView tv_time;

		private HomeNewsBean bean;

		OnClickListener click = new OnClickListener() {
			public void onClick(View v) {
				String id = (String) v.getTag();
				Intent i = new Intent(ctx, ProductInfoAct.class);
				i.putExtra("id", id);
				startActivity(i);
			}
		};

		public MyViewHolder(View view) {
			item_view = view.findViewById(R.id.click_view);
			img_logo = (ImageView) view.findViewById(R.id.img);
			tv_name = (TextView) view.findViewById(R.id.name);
			tv_part = (TextView) view.findViewById(R.id.partment);
			tv_time = (TextView) view.findViewById(R.id.time);
		}

		@Override
		public void setData(HomeNewsBean b) {
			this.bean = b;
			tv_name.setText(b.title);
			tv_part.setText(b.description);
			tv_time.setText(WKTimeUtils.formatTime(b.updatetime));
			app().imageLoader.displayImage(b.thumb, img_logo);
			final IntentBean mIBean = new IntentBean(b.title, b.thumb,
					b.description, b.inputtime, b.catid, b.id);
			item_view.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {

					Intent i = new Intent(ctx, NewsAct.class);
					i.putExtra("bean", mIBean);
					i.putExtra("title", "倍康资讯");
					ctx.startActivity(i);
				}
			});
		}

	}

}
